﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace SystemModule
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    public class TOAbility  //60
    {
        public ushort Level;  // 等级
        public ushort AC;
        public ushort MAC;
        public ushort DC;
        public ushort MC;
        public ushort SC;
        public int HP; // 21亿血
        public int MP;// 21亿魔法
        public int MaxHP;// 21亿血
        public int MaxMP; // 21亿魔法
        public ushort NG; // 当前内力值
        public ushort MaxNG; // 内力值上限
        public int Exp;// 当前经验
        public int MaxExp; // 升级经验
        public ushort Weight;// 最大重量
        public ushort MaxWeight;// 最大重量
        public int WearWeight;
        public int MaxWearWeight; // 最大负重
        public int HandWeight;
        public int MaxHandWeight;

        public TOAbility() { 
        
        }

        public TOAbility(byte[] buff)
        {
            Level = BitConverter.ToUInt16(buff, 0);
            AC = BitConverter.ToUInt16(buff, 2);
            MAC = BitConverter.ToUInt16(buff, 4);
            DC = BitConverter.ToUInt16(buff, 6);
            MC = BitConverter.ToUInt16(buff, 8);
            SC = BitConverter.ToUInt16(buff, 10);
            HP = BitConverter.ToInt32(buff, 12);
            MP = BitConverter.ToInt32(buff, 16);
            MaxHP = BitConverter.ToInt32(buff, 20);
            MaxMP = BitConverter.ToInt32(buff, 24);
            NG = BitConverter.ToUInt16(buff, 26);
            MaxNG = BitConverter.ToUInt16(buff, 28);
            Exp = BitConverter.ToInt32(buff, 32);
            MaxExp = BitConverter.ToInt32(buff, 36);
            Weight = BitConverter.ToUInt16(buff, 40);
            MaxWeight = BitConverter.ToUInt16(buff, 42);
            WearWeight = BitConverter.ToInt32(buff, 44);
            MaxWearWeight = BitConverter.ToInt32(buff, 48);
            HandWeight = BitConverter.ToInt32(buff, 52);
            MaxHandWeight = BitConverter.ToInt32(buff, 56);
        }

        public byte[] ToByte()
        {
            using (var memoryStream = new MemoryStream())
            {
                var backingStream = new BinaryWriter(memoryStream);
                backingStream.Write(Level);
                backingStream.Write(AC);
                backingStream.Write(MAC);
                backingStream.Write(DC);
                backingStream.Write(MC);
                backingStream.Write(SC);
                backingStream.Write(HP);
                backingStream.Write(MP);
                backingStream.Write(MaxHP);
                backingStream.Write(MaxMP);
                backingStream.Write(NG);
                backingStream.Write(MaxNG);
                backingStream.Write(Exp);
                backingStream.Write(MaxExp);
                backingStream.Write(Weight);
                backingStream.Write(MaxWeight);
                backingStream.Write(WearWeight);
                backingStream.Write(MaxWearWeight);
                backingStream.Write(HandWeight);
                backingStream.Write(MaxHandWeight);

                var stream = backingStream.BaseStream as MemoryStream;
                return stream.ToArray();
            }
        }
    }
}
